package com.cyms.modules.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.modules.sys.controller.AbstractController;
import com.cyms.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.base.entity.CompanyEntity;
import com.cyms.modules.base.entity.CustomerEntity;
import com.cyms.modules.base.service.CustomerService;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 客户档案
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-02 22:51:16
 */
@RestController
@RequestMapping("base/customer")
public class CustomerController extends AbstractController {
    @Autowired
    private CustomerService customerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("base:customer:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("createOrg",getUser().getCompanyId());
        PageUtils page = customerService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/selectList")
    @RequiresPermissions("base:customer:list")
    public R selectList(@RequestParam Map<String, Object> params) {
		// TODO Auto-generated method stub
    	List<CustomerEntity> list=customerService.selectList(new EntityWrapper<CustomerEntity>().eq("create_org",getUser().getCompanyId()));
    	List<Map<String,Object>> data=new ArrayList<>();
    	for (CustomerEntity customerEntity : list) {
    		Map<String, Object> map=new HashMap<>();
			map.put("value", customerEntity.getId());
			map.put("label", customerEntity.getName());
			//
			data.add(map);
		}
    	return R.ok().put("customerList", data);
	}
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("base:customer:info")
    public R info(@PathVariable("id") Long id){
			CustomerEntity customer = customerService.selectById(id);

        return R.ok().put("customer", customer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("base:customer:save")
    public R save(@RequestBody CustomerEntity customer){
    	customer.setCreateDate(new Date());
        SysUserEntity currentUser = getUser();
        customer.setCreateUser(currentUser.getUserId());
        customer.setCreateDept(currentUser.getDeptId());
        customer.setCreateOrg(currentUser.getCompanyId());
		customerService.insert(customer);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("base:customer:update")
    public R update(@RequestBody CustomerEntity customer){
			customerService.updateById(customer);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("base:customer:delete")
    public R delete(@RequestBody Long[] ids){
			customerService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
